package com.rjhch.bigDataCluterHelp;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.util.StringUtils;

/**
 * 放置一些公共组件工具
 */
public class CommonTools {
    private static final Log log = LogFactory.getLog(CommonTools.class);

    public static Configuration config;
    public static FileSystem hdfs;
    public static Admin admin;

    /**
     * 登录hdfs，此处配置写死，一般不需要改变
     */
    private static void login() throws Exception {
        if (UserGroupInformation.isSecurityEnabled()) {
            String principal = get("big-data-help.kerberos.principal");
            String keytabFile = get("big-data-help.keytab.file");
            log.info("kerberos principal: " + principal);
            log.info("keytab file: " + keytabFile);
            UserGroupInformation.loginUserFromKeytab(principal, keytabFile);
        }
    }

    /**
     * 初始化系统
     */
    public static void init() throws Exception {
        init(null);
    }

    /**
     * 初始化系统，可指定额外配置文件
     */
    public static void init(String... resourceFileNames) throws Exception {
        config = HBaseConfiguration.create();
        config.addResource("custom-core-site.xml");
        config.addResource("custom-hbase-site.xml");
        if (resourceFileNames != null && resourceFileNames.length != 0) {
            for (String resourceFileName : resourceFileNames) {
                config.addResource(resourceFileName);
            }
        }

        login();

        hdfs = FileSystem.get(config);
        Connection con = ConnectionFactory.createConnection(config);
        admin = con.getAdmin();
    }

    public static boolean getBoolean(String name, boolean defaultValue) {
        String valueString = System.getProperty(name);
        if (valueString != null) {
            log.debug("get property from system property. name: "+ name +", value: "+ valueString);
            return Boolean.parseBoolean(valueString);
        }
        return config.getBoolean(name, defaultValue);
    }

    public static int getInt(String name, int defaultValue) {
        String valueString = System.getProperty(name);
        if (valueString != null) {
            log.debug("get property from system property. name: "+ name +", value: "+ valueString);
            return Integer.parseInt(valueString);
        }
        return config.getInt(name, defaultValue);
    }

    public static long getLong(String name, long defaultValue) {
        String valueString = System.getProperty(name);
        if (valueString != null) {
            log.debug("get property from system property. name: "+ name +", value: "+ valueString);
            return Long.parseLong(valueString);
        }
        return config.getLong(name, defaultValue);
    }

    public static float getFloat(String name, float defaultValue) {
        String valueString = System.getProperty(name);
        if (valueString != null) {
            log.debug("get property from system property. name: "+ name +", value: "+ valueString);
            return Float.parseFloat(valueString);
        }
        return config.getFloat(name, defaultValue);
    }

    public static String get(String name) {
        return get(name, null);
    }

    public static String get(String name, String defaultValue) {
        String valueString = System.getProperty(name);
        if (valueString != null) {
            log.debug("get property from system property. name: "+ name +", value: "+ valueString);
            return valueString;
        }
        return config.get(name, defaultValue);
    }

    public static String[] getStrings(String name, String... defaultValue) {
        String valueString = System.getProperty(name);
        if (valueString != null) {
            log.debug("get property from system property. name: "+ name +", value: "+ valueString);
            return valueString.split(",");
        }
        return config.getStrings(name, defaultValue);
    }
}
